package com.longxi.tianjintianbao.ui.presenter.patrol;

import android.content.Intent;

import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.greedao.controller.DataPatrolController;
import com.longxi.tianjintianbao.greedao.entity.Patrol;
import com.longxi.tianjintianbao.listener.SelectPatrolListener;
import com.longxi.tianjintianbao.ui.activity.patrol.PatrolDetailActivity;
import com.longxi.tianjintianbao.ui.adapter.multitype.patrol.PatrolListViewBinder;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.view.patrol.IPatrolScanSearchAtView;
import com.longxi.tianjintianbao.utils.MyUtils;

import java.util.List;

import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;

public class PatrolScanSearchAtPresenter extends BasePresenter<IPatrolScanSearchAtView> {
    public static String ADDRESSID = "ADDRESSID";

    private Intent intent = new Intent();
    private MultiTypeAdapter adapter;
    private Items items;

    public PatrolScanSearchAtPresenter(BaseActivity context) {
        super(context);
    }

    public void initAdapter(String state){
        if(adapter == null){
            adapter = new MultiTypeAdapter();
            SelectPatrolListener listener = new SelectPatrolListener() {
                @Override
                public void onSuccess(Patrol patrol) {
                    Intent mainIntent = new Intent(mContext, PatrolDetailActivity.class);
                    mainIntent.putExtra(AppConst.BILLID, patrol.getBillid());
                    mainIntent.putExtra(AppConst.ISDRAFT, state);
                    mainIntent.putExtra(AppConst.ISSCAN, true);
                    mContext.startActivityForResult(mainIntent, AppConst.IntentRequstCode.ACTIVITY_PATROL_DETAIL);
                }
            };
            /* 注册类型和 View 的对应关系 */
            adapter.register(Patrol.class, new PatrolListViewBinder(listener));
            getView().getRvContent().setAdapter(adapter);
        }
    }

    public void setBar() {
        mContext.setTitle("扫一扫结果");
        mContext.setTvLeft(MyUtils.getString(R.string.home));
    }

    public void getScanSearchList() {
        Intent intent = mContext.getIntent();
        long addressid = Long.valueOf(intent.getStringExtra(ADDRESSID));

        List<Patrol> patrols = DataPatrolController.SelectByAddressid(addressid, "0");
        if(patrols.size() > 0){
            items = new Items();
            items.addAll(patrols);
            adapter.setItems(items);
            adapter.notifyDataSetChanged();
            mContext.hideLoading();
        }else{
            mContext.showNoData();
        }
    }

    /**
     * 发送广播
     */
    public void sendBroadcast(){
        //设置广播的名字（设置Action）
        intent.setAction(AppConst.REFRESH_HOME_FRAGMENT);
        //携带数据
        //intent.putExtra("refreshPatrolCount",true);
        //发送广播（无序广播）
        mContext.sendBroadcast(intent);
    }
}
